/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package guessflags;

/**
 *
 * @author Administrator
 */
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.*;
import java.awt.event.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


import java.util.*;

public class GuessFlagsGame extends JFrame {

    private JComboBox comboBox;
    Random random = new Random();
    int x = Math.round(random.nextInt(10));
    Image image = Toolkit.getDefaultToolkit().getImage("img\\" + x + ".jpg");
    Icon icon = new ImageIcon(image);
    Connection c;
    Statement st;

    public GuessFlagsGame() {
        super();
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String URL = "jdbc:mysql://194.47.47.18:3306/DA5221_GRP4?user=DA5221_GRP4&password=Fu393m";
            c = (Connection) DriverManager.getConnection(URL);
            st = (Statement) c.createStatement();
            final JLabel label_1 = new JLabel();
            final JLabel label_2 = new JLabel();
            final JLabel label_3 = new JLabel();
            final JLabel label_4 = new JLabel();
            final JLabel label_5 = new JLabel();


            label_1.setBounds(190, 60, 124, 81);
            label_1.setText("make decision");
            label_2.setBounds(190, 85, 124, 81);

            label_3.setBounds(190, 110, 124, 81);

            label_4.setBounds(190, 135, 124, 81);

            label_5.setBounds(190, 160, 124, 81);
            
            getContentPane().add(label_1);
            getContentPane().add(label_2);
            getContentPane().add(label_3);
            getContentPane().add(label_4);
            getContentPane().add(label_5);
            final JLabel lab_gq = new JLabel();
            lab_gq.setText("Flag showing");
            lab_gq.setBounds(41, 26, 85, 27);
            getContentPane().add(lab_gq);
            final JLabel lab_sf = new JLabel();
            lab_sf.setText("CHOOSE");
            lab_sf.setBounds(190, 26, 86, 27);
            getContentPane().add(lab_sf);
            final JLabel jlabel = new JLabel();
            jlabel.setBounds(10, 59, 174, 123);
            getContentPane().add(jlabel);
            getContentPane().setLayout(null);
            setBounds(100, 100, 496, 228);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JButton btn_xz = new JButton();
            btn_xz.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == btn_xz) {
                        int choose = comboBox.getSelectedIndex();
                        if (choose == x) {
                            try {
                                javax.swing.JOptionPane.showMessageDialog(null, "Congratulations，Correct answer！");
                                icon = new ImageIcon(image);
                                //label_1.setIcon(icon);
                                String sql1 = "SELECT population FROM world WHERE id = " + x;
                                ResultSet res1 = st.executeQuery(sql1);
//                                String sql2 = "SELECT country FROM world WHERE id = " + x;
//                                ResultSet res2 = st.executeQuery(sql2);
//                                String sql3 = "SELECT languages FROM world WHERE id = " + x;
//                                ResultSet res3 = st.executeQuery(sql3);
//                                String sql4 = "SELECT capital FROM world WHERE id = " + x;
//                                ResultSet res4 = st.executeQuery(sql4);
//                                String sql5 = "SELECT area FROM world WHERE id = " + x;
//                                 ResultSet res5 = st.executeQuery(sql5);
                                String pop;
                                String country;
                                String language;
                                String capital;
                                String area;
                                if (res1.next()) {
                                    pop = res1.getString(1);
                                    label_1.setText("pop:" + pop);
                                    
                                }
//                             else if (res2.next()) {
//                                    country = res2.getString(2);
//                                    label_2.setText("country:" + country);
//
//                                }
//                                else if (res3.next()) {
//                                    language = res3.getString(3);
//                                    label_3.setText("lan:" + language);
//
//                                }
//                                else if (res4.next()) {
//                                    capital = res4.getString(4);
//                                    label_4.setText("lan:" + capital);
//
//                                }
//                                else {
//                                    area = res5.getString(5);
//                                    label_5.setText("area:" + area);
//                                }

                            } catch (SQLException ex) {
                                Logger.getLogger(GuessFlagsGame.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    } else {
                        javax.swing.JOptionPane.showMessageDialog(null, "Congratulations，Wrong answer！");
                        label_1.setIcon(null);
                    }
                }

            });
            btn_xz.setText("Submit Answer");
            btn_xz.setBounds(354, 46, 124, 25);
            getContentPane().add(btn_xz);
            final JButton btn_gq = new JButton();
            btn_gq.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == btn_gq) {
                        x = Math.round(random.nextInt(10));
                        image = Toolkit.getDefaultToolkit().getImage("img\\" + x + ".jpg");
                        icon = new ImageIcon(image);
                        jlabel.setIcon(icon);
                    }
                }
            });
            btn_gq.setText("Start/Question");
            btn_gq.setBounds(354, 129, 124, 25);
            getContentPane().add(btn_gq);
            comboBox = new JComboBox();
            comboBox.setBounds(191, 59, 124, 24);
            getContentPane().add(comboBox);
            comboBox.addItem("China");
            comboBox.addItem("Brazil");
            comboBox.addItem("North Korea");
            comboBox.addItem("South Korea");
            comboBox.addItem("Canada");
            comboBox.addItem("Lithuania");
            comboBox.addItem("US");
            comboBox.addItem("Mongolia");
            comboBox.addItem("Turkey");
            comboBox.addItem("England");
        } catch (SQLException ex) {
            Logger.getLogger(GuessFlagsGame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GuessFlagsGame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(GuessFlagsGame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GuessFlagsGame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
